package com.kuzmich.locator;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.kuzmich.locator.location.MyLocationManager;

/**
 * Sample Application to demonstrate how to use some of the android Location
 * 
 * API's
 * Author: Jason Hudgins <jason@droidworks.com>
 */
public class TrivialGPS extends MapActivity
{
	private static final boolean DEBUG_MODE = true;
	private static final String MAP_API_KEY =
	  "0gnb440wcg0mGDTmJtOT3SDv2GXu07NqhdFeS6A";
	private static final String DEBUG_MAP_API_KEY =
	  "0gnb440wcg0lyeyGqdxRlIOSJALN_j--0eumRiw";

	private enum MyViewOption {
		VIEW_SATELLITE, VIEW_MAP, VIEW_STREET
	};

	private MapController mapController;
	private MapView mapView;
	// private MyLocationOverlay locationOverlay;
	private MyLocationManager locationManager;
	private GeoPoint curPosition;
	private LocationListener listener;
  private MyViewOption viewSelected;
  private String provider;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);

		// create a map view
		mapView = new MapView(this, DEBUG_MODE ? DEBUG_MAP_API_KEY : MAP_API_KEY);
		mapController = mapView.getController();

		mapView.getController().setZoom(15);
		mapView.setClickable(true);
		mapView.setEnabled(true);
		mapView.setSatellite(true); viewSelected = MyViewOption.VIEW_SATELLITE;
		mapView.setBuiltInZoomControls(true);

		setContentView(mapView);

		listener = new LocationUpdateHandler();
		// get a hangle on the location manager
		locationManager = MyLocationManager.getInstance(this);
		provider = LocationManager.GPS_PROVIDER;
		locationManager.requestLocationUpdates(provider, 0, 0, listener);
	}

	// this inner class is the intent reciever that recives notifcations
	// from the location provider about position updates, and then redraws
	// the MapView with the new location centered.
	public class LocationUpdateHandler implements LocationListener
	{
		private static final String TAG = "TGPS-listener";

		@Override
		public void onLocationChanged(Location loc)
		{
			int lat = (int) (loc.getLatitude() * 1E6);
			int lng = (int) (loc.getLongitude() * 1E6);
			curPosition = new GeoPoint(lat, lng);
			mapController.animateTo(curPosition);
			Log.v(TAG, "Current position is : " +
				"(lng, lat) = (" + lng + ", " + lat + ")");
			setContentView(mapView);
		}

		@Override
		public void onProviderDisabled(String provider)
		{
		}

		@Override
		public void onProviderEnabled(String provider)
		{
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras)
		{
		}
	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}

	private static class MenuEnums
	{
		public static final int MY_LOCATION = 1;
		public static final int SELECT_VIEW = 2;
		public static final int SELECT_PROVIDER = 3;
		public static final int QUIT = 4;

		public static final int SELECT_VIEW_GROUP_ID = 10;
		public static final int SELECT_MAP_VIEW = 11;
		public static final int SELECT_STREET_VIEW = 12;
		public static final int SELECT_SATELLITE_VEW = 13;

		public static final int SELECT_PROVIDER_GID = 20;
		public static final int SELECT_GPS = 21;
		public static final int SELECT_CELLID = 22;
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0, MenuEnums.MY_LOCATION, 0, "My Location");
		SubMenu view = menu.addSubMenu(0, MenuEnums.SELECT_VIEW, 0, "Select View");
		menu.add(0, MenuEnums.QUIT, 0, "Quit");

		view.add(MenuEnums.SELECT_VIEW_GROUP_ID, MenuEnums.SELECT_MAP_VIEW, 0,
		  "Map").setChecked(viewSelected == MyViewOption.VIEW_MAP);
		view.add(MenuEnums.SELECT_VIEW_GROUP_ID, MenuEnums.SELECT_SATELLITE_VEW, 0,
		  "Satellite").setChecked(viewSelected == MyViewOption.VIEW_SATELLITE);
		view.add(MenuEnums.SELECT_VIEW_GROUP_ID, MenuEnums.SELECT_STREET_VIEW, 0,
		  "Street").setChecked(viewSelected == MyViewOption.VIEW_STREET);
		view.setGroupCheckable(MenuEnums.SELECT_VIEW_GROUP_ID, true, true);

		{
			boolean isGps = this.provider.equals(MyLocationManager.GPS_PROVIDER);

			SubMenu provider = menu.addSubMenu(0, MenuEnums.SELECT_PROVIDER, 0, "Select Provider");
			provider.add(MenuEnums.SELECT_PROVIDER_GID, MenuEnums.SELECT_GPS, 0, "GPS")
			  .setChecked(isGps);
			provider.add(MenuEnums.SELECT_PROVIDER_GID, MenuEnums.SELECT_CELLID, 0, "CellID")
			  .setChecked(!isGps);
			provider.setGroupCheckable(MenuEnums.SELECT_PROVIDER_GID, true, true);
		}

		return true;
	}

  private boolean onOptionsViewSelected(MenuItem item)
  {
		switch (item.getItemId())
		{
		case MenuEnums.SELECT_MAP_VIEW:
		case MenuEnums.SELECT_SATELLITE_VEW:
		case MenuEnums.SELECT_STREET_VIEW:
	    if (item.isChecked())
	    	item.setChecked(false);
	    else
	    	item.setChecked(true);
		}

		switch (item.getItemId())
		{
		case MenuEnums.SELECT_MAP_VIEW:
			mapView.setStreetView(false);
			mapView.setSatellite(false);
			setContentView(mapView);
			return true;
		case MenuEnums.SELECT_SATELLITE_VEW:
			mapView.setSatellite(true);
			mapView.setStreetView(false);
			setContentView(mapView);
			return true;
		case MenuEnums.SELECT_STREET_VIEW:
			mapView.setStreetView(true);
			mapView.setSatellite(false);
			setContentView(mapView);
			return true;
		}
		return false;
  }

	private boolean onOptionsProviderSelected(MenuItem item)
  {
		switch (item.getItemId())
		{
		case MenuEnums.SELECT_GPS:
		case MenuEnums.SELECT_CELLID:
	    if (item.isChecked())
	    	item.setChecked(false);
	    else
	    	item.setChecked(true);
		}

		switch (item.getItemId())
		{
		case MenuEnums.SELECT_GPS:
			if (locationManager.isProviderEnabled(MyLocationManager.NETWORK_PROVIDER))
				locationManager.removeUpdates(listener);
			if (!locationManager.isProviderEnabled(MyLocationManager.GPS_PROVIDER))
			{
				Toast.makeText(this, "Sorry, GPS service is not available. " +
					"Please turn it on in your Android Settings menu.",
					Toast.LENGTH_LONG).show();
				return true;
			}

			locationManager.requestLocationUpdates(MyLocationManager.GPS_PROVIDER,
				0, 0, listener);
			return true;
		case MenuEnums.SELECT_CELLID:
			if (locationManager.isProviderEnabled(MyLocationManager.GPS_PROVIDER))
				locationManager.removeUpdates(listener);
			if (!locationManager.isProviderEnabled(MyLocationManager.NETWORK_PROVIDER))
			{
				Toast.makeText(this, "Sorry, CellId service is not available. " +
					"It requires an Internet connection. Please mark a corresponding " +
					"checkbox in application preferences.",
					Toast.LENGTH_LONG).show();
				return true;
			}

			locationManager.requestLocationUpdates(MyLocationManager.NETWORK_PROVIDER,
				0, 0, listener);
			return true;
		}
	  return false;
  }

	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getGroupId())
		{
		case MenuEnums.SELECT_VIEW_GROUP_ID:
			return onOptionsViewSelected(item);
		case MenuEnums.SELECT_PROVIDER_GID:
			return onOptionsProviderSelected(item);
		}
		switch (item.getItemId())
		{
		case MenuEnums.MY_LOCATION:
			if (curPosition == null)
			{
				Toast t =
				  Toast.makeText(getApplicationContext(), "Waiting for location",
				    Toast.LENGTH_LONG);
				t.setGravity(Gravity.BOTTOM, 0, 0);
				t.show();
			}
			else
				mapController.animateTo(curPosition);
			setContentView(mapView);
			return true;
		case MenuEnums.QUIT:
			locationManager.removeUpdates(listener);
			finish();
			return true;
		}
		return false;
  }
}